package com.iip.fabric.controller;


import com.alibaba.fastjson.JSONObject;
import com.iip.fabric.pojo.BlockInfo;
import com.iip.fabric.pojo.Chain;
import com.iip.fabric.service.ChainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hyperledger.fabric.gateway.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.cert.CertificateException;

@Api(tags = "区块链接口管理")
@RestController
@RequestMapping("/admin")
//跨域问题
@CrossOrigin
public class ChainController {

    @Autowired
    private ChainService chainService;

    @ApiOperation(value = "上链文件信息")
    @PostMapping("upChain")
    public String upChain(@RequestBody(required = false) String nothing, String key,String name,String size,String uptime,String owner,String operation){
        boolean valid = chainService.create(key, name, size, uptime, owner, operation);
        if(valid){
            System.out.println(nothing);
            System.out.println(key);
            System.out.println(name);
            System.out.println(size);
            System.out.println(uptime);
            System.out.println(owner);
            System.out.println(operation);
            return  "上链成功 数据如下："+
                    "key:" + key+"    "+
                    "name:" + name+"    "+
                    "size:" + size+"    "+
                    "uptima:" + uptime+"    "+
                    "owner:" + owner+"    "+
                    "operation:" + operation+"    ";

        }else{
            return "上链失败";
        }

    }


    @ApiOperation(value = "查询指定链上文件信息")
    @RequestMapping(value = "query",method = RequestMethod.GET)
    public R ChainQuery(@RequestParam String key, @RequestParam(value = "startTime",required = false) String startTime, @RequestParam(value = "endTime", required = false) String endTime)throws CertificateException, ContractException, InvalidKeyException, IOException{

        //查链
        byte[] result = chainService.query(key);
        String res;
        Chain chain;
        if (result != null){
            res = new String(result);
            System.out.println(res);
            JSONObject jsonObject = JSONObject.parseObject(res);
//            System.out.println(jsonObject);
            chain = new Chain(jsonObject.get("name").toString(),jsonObject.get("name").toString(),jsonObject.get("size").toString(),jsonObject.get("uptime").toString(),jsonObject.get("owner").toString(),"query");
        } else {
            String info = "不存在该条记录！";
            return R.error(info);
        }

        return R.ok(chain);

    }


    //处理历史记录查询请求
    @ApiOperation(value = "查询文件历史记录")
    @RequestMapping(value = "historyquery/{key}", method = RequestMethod.GET)
    public String historyQuery(@PathVariable("key") String key)throws CertificateException, ContractException, InvalidKeyException, IOException{

        //历史查询
        byte[] result = chainService.queryfilehistory(key);
        String res = new String();
        if (result != null){
            res = new String(result);
            System.out.println(res);
        }
        return res;
    }

    //删除链上信息
    @ApiOperation(value = "删除链上信息")
    @RequestMapping(value = "delete/{key}", method = RequestMethod.GET)
    public String delete(@PathVariable("key") String key)throws CertificateException, ContractException, InvalidKeyException, IOException{

        byte[] result = chainService.delete(key);
        String res = new String();
        if (result != null){
            res = new String(result);
            System.out.println(res);
        }
        return res;
    }
    //查询区块信息
    @ApiOperation(value = "查询区块信息")
    @RequestMapping(value = "channelInfo", method = RequestMethod.GET)
    public R channelInfo()throws CertificateException, ContractException, InvalidKeyException, IOException{

        //历史查询
        String[] result = chainService.channelInfo();
        BlockInfo blockInfo = new BlockInfo();
        blockInfo.setBlockHigh(result[0]);
        blockInfo.setPreHash(result[1]);
        blockInfo.setCurHash(result[2]);


        return R.ok(blockInfo);
    }

    //查询区块信息
    @ApiOperation(value = "查询区块信息-指定区块高度")
    @RequestMapping(value = "blockInfo/{height}", method = RequestMethod.GET)
    public R blockInfo(@PathVariable("height") long height) throws CertificateException, ContractException, InvalidKeyException, IOException{

        //历史查询
        String result = chainService.blockInfo(height);
        BlockInfo blockInfo = new BlockInfo();
        blockInfo.setTransactionCount(result);


        return R.ok(blockInfo);
    }

    @ApiOperation(value = "测试1")
    @PostMapping("test1")
    public Result test1(@RequestBody(required = false) Chain chain){
        boolean valid = true;
//        boolean valid = chainService.create(chain.getKey(), chain.getName(), chain.getSize(), chain.getUptime(), chain.getOwner(), chain.getOperation());
//        System.out.println(chain.getSize());

//        String key = chain.
//        System.out.println(chain.key());
        if(valid){
            System.out.println(chain.getKey());
            System.out.println(chain.getName());
            System.out.println(chain.getSize());
            System.out.println(chain.getUptime());
        }

        return Result.ok(chain);
    }


    @ApiOperation(value = "测试")
    @PostMapping("test")
    public String test(@RequestBody(required = false) String key)throws CertificateException, ContractException, InvalidKeyException, IOException{

        System.out.println("远程调用"+key);

        return "成功远程调用";
    }


}
